package com.dwxt.community.service;

import com.dwxt.common.base.*;
import com.dwxt.common.util.*;
import com.dwxt.community.dao.*;
import com.dwxt.community.entity.*;
import org.apache.commons.lang.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.stereotype.*;
import org.springframework.transaction.annotation.*;

import java.text.*;
import java.util.*;

@Service
public class RecycleService {
    @Autowired
    private RecycleAddressMapper recycleAddressMapper;

    @Autowired
    private RecycleRecordMapper recycleRecordMapper;

    @Transactional
    public void createOrUpdateAddress(RecycleAddress recycleAddress) {
        String userId = PartyUtil.getCurrentUserId();
        //修改其他地址为非默认
        if (recycleAddress.getIsDefault() == YesOrNo.YES) {
            updateAddressToNotDefault(userId);
        }
        if (recycleAddress.getId() != null) {
            recycleAddressMapper.updateByPrimaryKeySelective(recycleAddress);
        } else {
            recycleAddress.setUserId(userId);
            recycleAddressMapper.insertSelective(recycleAddress);
        }
    }

    private void updateAddressToNotDefault(String userId) {
        RecycleAddress address = new RecycleAddress();
        address.setIsDefault(YesOrNo.NO);
        RecycleAddressExample example = new RecycleAddressExample();
        example.createCriteria().andUserIdEqualTo(userId).andIsDefaultEqualTo(YesOrNo.YES);
        recycleAddressMapper.updateByExampleSelective(address, example);
    }

    public List<RecycleAddress> selectAddressList() {
        String userId = PartyUtil.getCurrentUserId();
        RecycleAddressExample example = new RecycleAddressExample();
        example.createCriteria().andUserIdEqualTo(userId);
        example.setOrderByClause("create_time desc");
        return recycleAddressMapper.selectByExample(example);
    }

    /**
     * 上门回收记录
     * @param recycleRecord
     * @return
     */
    public Integer createRecycleRecord(RecycleRecord recycleRecord) {
        recycleRecord.setIsCompleted(YesOrNo.NO);
        recycleRecord.setUserId(PartyUtil.getCurrentUserId());
        return recycleRecordMapper.insertSelective(recycleRecord);
    }

    public List<RecycleRecord> selectList() {
        List<RecycleRecord> recycleRecords = recycleRecordMapper.selectByExample(new RecycleRecordExample());
        if (recycleRecords.size()>0){
            return recycleRecords;
        }else {
            return null;
        }
    }


    public RecycleAddress selectDefaultAddress() {
        String userId = PartyUtil.getCurrentUserId();
        RecycleAddressExample example = new RecycleAddressExample();
        example.createCriteria().andUserIdEqualTo(userId).andIsDefaultEqualTo(YesOrNo.YES);
        List<RecycleAddress> recycleAddressList = recycleAddressMapper.selectByExample(example);
        if (recycleAddressList.size() > 0) {
            return recycleAddressList.get(0);
        } else {
            example.clear();
            example.createCriteria().andUserIdEqualTo(userId);
            example.setOrderByClause("create_time desc");
            recycleAddressList = recycleAddressMapper.selectByExample(example);
            if (recycleAddressList.size() > 0) {
                return recycleAddressList.get(0);
            } else {
                return null;
            }
        }
    }

    public List<RecycleRecord> selectByQuery(String userName, String communityName, String time) {
        RecycleRecordExample recycleRecordExample = new RecycleRecordExample();
        RecycleRecordExample.Criteria criteria = recycleRecordExample.createCriteria();
            if (StringUtils.isNotEmpty(communityName)){
                criteria.andCommunityNameEqualTo(communityName);
            }
            if (StringUtils.isNotEmpty(userName)){
                criteria.andUsernameEqualTo(userName);
            }
            if (StringUtils.isNotEmpty(time)){
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    Date parse = sdf.parse(time);
                    criteria.andAppointTimeEqualTo(parse);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
        List<RecycleRecord> recycleRecords = recycleRecordMapper.selectByExample(recycleRecordExample);
            return recycleRecords;
    }

    public RecycleAddress selectByKey(Integer id) {
        return recycleAddressMapper.selectByPrimaryKey(id);
    }

    public void deleteAddressByKey(Integer id) {
        recycleAddressMapper.deleteByPrimaryKey(id);
    }
}
